﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSDBM.Code;
using System.Data;
using VSDBM.Code.dao;

public partial class Visual_Procedures_Reports_GeneralMapAndSub_Report : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillOrganism();
        }
    }
    protected void FillOrganism()
    {
        DataSet ds = OrganismDAO.select("WHERE status=1", "ORDER BY name");
        //verifica erro na querry
        if (ds.Tables.Count == 0)
        {
            Exception ex = new Exception("Table not found!");
            throw ex;
        }
        //verifica se existem organismos cadastrados.
        else if (ds.Tables[0].Rows.Count == 0)
        {
            DataRow dr = ds.Tables[0].NewRow();
            ds.Tables[0].Rows.InsertAt(dr, 0);
            ds.Tables[0].Rows[0]["name"] = "NO ORGANISMS FOUND";
            ds.Tables[0].Rows[0]["id"] = "0";

        }
        //Insere os organismos no Dropdown List(ddOrganism).

        ddOrganism.DataSource = ds.Tables[0];
        ddOrganism.DataTextField = "name";
        ddOrganism.DataValueField = "id";
        ddOrganism.DataBind();
        Refresh();

    }

    protected void ddOrganism_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT     sequences.idacession, sequences.definition AS sequencedefinition, sequence_map.qualifier, sequence_map.idfeature, reference_features.description AS featuredescription, sequences.idorganism, subtypes.description AS subtypedescription FROM sequences INNER JOIN sequence_map ON sequences.id = sequence_map.idsequence INNER JOIN reference_features ON sequence_map.idfeature = reference_features.id INNER JOIN sequence_subtype ON sequences.id = sequence_subtype.idsequence INNER JOIN subtypes ON sequence_subtype.idsubtype = subtypes.id WHERE (sequences.idorganism=" + Convert.ToInt32(ddOrganism.SelectedValue) + ")";
        SqlDataSource1.Select(new DataSourceSelectArguments());
        ReportViewer1.LocalReport.Refresh();
    }
    protected void Refresh()
    {

        SqlDataSource1.SelectCommand = "SELECT     sequences.idacession, sequences.definition AS sequencedefinition, sequence_map.qualifier, sequence_map.idfeature, reference_features.description AS featuredescription, sequences.idorganism, subtypes.description AS subtypedescription FROM sequences INNER JOIN sequence_map ON sequences.id = sequence_map.idsequence INNER JOIN reference_features ON sequence_map.idfeature = reference_features.id INNER JOIN sequence_subtype ON sequences.id = sequence_subtype.idsequence INNER JOIN subtypes ON sequence_subtype.idsubtype = subtypes.id WHERE (sequences.idorganism=" + Convert.ToInt32(ddOrganism.SelectedValue) + ")";
        SqlDataSource1.Select(new DataSourceSelectArguments());
        ReportViewer1.LocalReport.Refresh();

    }
    protected void ReportViewer1_ReportRefresh(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Refresh();
    }
}